<template>
  <div>
    <div>
      <el-form :inline="true" :model="searForm" class="demo-form-inline">
        <el-form-item label="关键词">
          <el-input
            v-model="searForm.keyWord"
            placeholder="请输入关键词"
            clearable
          />
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select
            v-model="searForm.statusId"
            placeholder="请输入订单状态 "
            style="width: 150px"
            clearable
          >
            <el-option label="待支付" :value="0" />
            <el-option label="已支付" :value="1" />
            <el-option label="待发货 " :value="2" />
            <el-option label="已签收 " :value="3" />
          </el-select>
        </el-form-item>

        <el-form-item label="订单类型">
          <el-select
            v-model="searForm.typeId"
            placeholder="请输入订单类型"
            style="width: 150px"
            clearable
          >
            <el-option label="普通订单" :value="0" />
            <el-option label="秒杀订单" :value="1" />
            <el-option label="团购订单" :value="2" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="localOrderList()">搜索</el-button>
          <el-button
            type="danger"
            :disabled="multipleSelection.length == 0"
            @click="onDelAll()"
            >批量删除</el-button
          >
          <el-button type="success" @click="onExport()">批量导出</el-button>
          <el-button type="success" @click="onShowAddOrder()"
            >新增订单</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
  <div>
    <h1>订单列表</h1>
    <el-table
      :data="tableData"
      stripe
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="orderId" label="ID" width="50" />
      <el-table-column prop="orderNo" label="订单编号" />
      <el-table-column prop="orderType" label="类型">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.orderType == 0"
            >普通订单</el-tag
          >
          <el-tag type="primary" v-if="scope.row.orderType == 1"
            >秒杀订单</el-tag
          >
          <el-tag type="primary" v-if="scope.row.orderType == 2"
            >团购订单</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="realName" label="买家" />
      <el-table-column prop="prodInfo" label="商品名称" />
      <el-table-column prop="amount" label="金额" width="80" />
      <el-table-column prop="orderStatus" label="状态" width="80">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.orderStatus == 0"
            >待支付</el-tag
          >
          <el-tag type="primary" v-if="scope.row.orderStatus == 1"
            >已支付</el-tag
          >
          <el-tag type="primary" v-if="scope.row.orderStatus == 2"
            >待发货</el-tag
          >
          <el-tag type="primary" v-if="scope.row.orderStatus == 3"
            >已签收</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="prodNum" label="数量" width="80" />
      <el-table-column label="图片" width="80px">
        <template #default="scope">
          <el-image
            v-for="item in scope.row.imageList"
            style="width: 30px; height: 30px"
            :src="item"
            :key="item"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-pagination
      v-model:current-page="searForm.pageNum"
      v-model:page-size="searForm.pageSize"
      :page-sizes="[2, 4, 6, 8]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <!-- 新增订单 -->
  <div>
    <el-dialog v-model="dialogFormVisible" title="添加订单" width="1000">
      <el-form :model="form">
        <el-form-item label="选择商品" :label-width="formLabelWidth">
          <el-select
            v-model="form.prodId"
            @change="onChangeProd"
            placeholder="请选择商品"
          >
            <el-option
              v-for="item in prodList"
              :label="item.prodName"
              :value="item.prodId"
              :key="item.prodId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="Zones" :label-width="formLabelWidth">
          <el-table
            :data="prodInfoList"
            stripe
            style="width: 100%"
            @selection-change="onHandleSelectionChange"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column prop="infoId" label="ID" width="180" />
            <el-table-column prop="prodName" label="商品名称" width="180" />
            <el-table-column prop="prodPrice" label="商品价格" width="180" />
            <el-table-column prop="buyNum" label="购买数量" width="180" />
            <el-table-column label="操作" />
          </el-table>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">放弃</el-button>
          <el-button type="primary" @click="onsubmitAddProd"> 购买 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
  <div>
    <el-drawer
      v-model="table"
      title="I have a nested table inside!"
      direction="rtl"
      size="50%"
    >
      <el-table :data="recordData">
        <el-table-column property="recordId" label="序号" width="150" />
        <el-table-column
          property="recordContent"
          label="操作记录"
          width="200"
        />
        <el-table-column property="recordTime" label="操作时间" width="200" />
        <el-table-column label="操作" />
      </el-table>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import {
  getOrderList,
  delOrderAll,
  getProdList,
  getProdInfoList,
  addOrder,
  getOrderRecordList,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  name: "",
  region: "",
  date1: "",
  date2: "",
  delivery: false,
  type: [],
  resource: "",
  desc: "",
  prodId: null,
});
const table = ref(false);
const tableData = ref([]);
const recordData = ref([]);
const total = ref(0);
const searForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
  typeId: null,
  statusId: null,
});
const loadOrderList = () => {
  getOrderList(searForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searForm.value.pageSize = val;
  loadOrderList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searForm.value.pageNum = val;
  loadOrderList();
};
const localOrderList = () => {
  loadOrderList();
};

//多选框
const multipleSelection = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log("multipleSelection.value", multipleSelection.value);
};
const onDelAll = () => {
  console.log("onDelAll", multipleSelection.value);
  if (multipleSelection.value.length == 0) {
    ElMessage.error("请选择要删除的信息");
    return;
  }
  console.log(multipleSelection.value);
  ElMessageBox.confirm("删除后信息无法恢复，确定删除吗?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "放弃",
    type: "warning",
  })
    .then(() => {
      //批量删除
      delOrderAll(multipleSelection.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadOrderList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

//批量导出
const onExport = () => {
  let ids = "";
  tableData.value.forEach((item) => {
    if (ids == "") {
      ids = item.orderId;
    } else {
      ids = ids + "," + item.orderId;
    }
  });

  let url = "http://localhost:9011/shop/order/exportOderExcel/" + ids;
  window.open(url);
};

//展示新增订单
const onShowAddOrder = () => {
  dialogFormVisible.value = true;
};
const prodInfoList = ref([]);
const onChangeProd = (value: any) => {
  console.log("orderList", value);
  getProdInfoList({ pageNum: 1, pageSize: 1000, typeId: value }).then((res) => {
    if (res.data.code == 200) {
      prodInfoList.value = res.data.data.records;

      if (prodInfoList.value.length > 0) {
        prodInfoList.value.forEach((item) => {
          item.buyNum = 1;
        });
      }
    }
  });
};

const onMultipleSelection = ref<any[]>([]);
const onHandleSelectionChange = (val: any[]) => {
  onMultipleSelection.value = val;
  console.log(onMultipleSelection.value);
};

const onsubmitAddProd = () => {
  if (onMultipleSelection.value.length == 0) {
    ElMessage.error("请选择商品");
    return;
  }
  addOrder(onMultipleSelection.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      loadOrderList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const handleEdit = (index: number, row: any) => {
  console.log(index, row);
  getOrderRecordList(row.orderId).then((res) => {
    if (res.data.code == 200) {
      recordData.value = res.data.data;
      table.value = true;
    }
  });
};

const prodList = ref([]);

onMounted(() => {
  loadOrderList();
  getProdList({ pageNum: 1, pageSize: 1000 }).then((res) => {
    if (res.data.code == 200) {
      prodList.value = res.data.data.records;
    }
  });
});
</script>
